package org.bonorsoft.modules.system.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.common.util.DateUtils;
import org.bonorsoft.modules.system.entity.SysDictItem;
import org.bonorsoft.modules.system.service.ISysDictItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.bonorsoft.modules.system.service.ISysDictService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

 /**
 * @Description: 对应字典类别下的字典值
 * @Author: WilliamYe
 * @Date:   2020-11-18
 * @Version: V1.0
 */
@Slf4j
@Controller
@RequestMapping("/system/dict/data")
public class SysDictItemController extends BonorController<SysDictItem, ISysDictItemService> {
	@Autowired
	private ISysDictItemService sysDictItemService;

	@Autowired
	private ISysDictService dictService;

	private String prefix = "system/dict/data";

	 @RequiresPermissions("system:dict:view")
	 @GetMapping()
	 public String dictData()
	 {
		 return prefix + "/data";
	 }

	/**
	 * 分页列表查询
	 *
	 * @param sysDictItem
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "对应字典类别下的字典值-分页列表查询")
	@RequiresPermissions("system:dict:list")
	@GetMapping(value = "/list")
	@ResponseBody
	public Result<?> queryPageList(SysDictItem sysDictItem,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SysDictItem> queryWrapper = QueryGenerator.initQueryWrapper(sysDictItem, req.getParameterMap());
		Page<SysDictItem> page = new Page<SysDictItem>(pageNo, pageSize);
		IPage<SysDictItem> pageList = sysDictItemService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	 /**
	  * 新增字典类型
	  */
	 @GetMapping("/add/{dictId}")
	 public String add(@PathVariable("dictId") String dictId, ModelMap mmap)
	 {
		 String dictCode = dictService.getDictCode(dictId);
		 mmap.put("dictCode",dictCode);
		 mmap.put("dictId", dictId);
		 return prefix + "/add";
	 }

	/**
	 * 添加
	 *
	 * @param sysDictItem
	 * @return
	 */
	@AutoLog(value = "对应字典类别下的字典值-添加")
	@RequiresPermissions("system:dict:add")
	@PostMapping("/add")
	@ResponseBody
	public Result<?> add(@Validated SysDictItem sysDictItem) {
		sysDictItem.setCreateBy(getLoginUserName());
		sysDictItem.setCreateTime(DateUtils.getDate());
		sysDictItemService.save(sysDictItem);
		return Result.ok("添加成功！");
	}

	 /**
	  * 编辑页面
	  */
	 @GetMapping("/edit/{id}")
	 public String edit(@PathVariable("id") String id, ModelMap mmap)
	 {
		 SysDictItem dictItem = sysDictItemService.getById(id);
		 String dictCode = dictService.getDictCode(dictItem.getDictId());
		 mmap.put("dict", dictItem);
		 mmap.put("dictCode",dictCode);
		 return prefix + "/edit";
	 }

	/**
	 * 编辑
	 *
	 * @param sysDictItem
	 * @return
	 */
	@AutoLog(value = "对应字典类别下的字典值-编辑")
	@RequiresPermissions("system:dict:edit")
	@PostMapping(value = "/edit")
	@ResponseBody
	public Result<?> edit(@Validated SysDictItem sysDictItem) {
		sysDictItemService.updateById(sysDictItem);
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "对应字典类别下的字典值-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		sysDictItemService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "对应字典类别下的字典值-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	@RequiresPermissions("system:dict:remove")
	@ResponseBody
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sysDictItemService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "对应字典类别下的字典值-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		SysDictItem sysDictItem = sysDictItemService.getById(id);
		return Result.ok(sysDictItem);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param sysDictItem
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, SysDictItem sysDictItem) {
      return super.exportXls(request, sysDictItem, SysDictItem.class, "对应字典类别下的字典值");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, SysDictItem.class);
  }

}
